#include "JProjectView.h"
#include "JProjectModel.h"
#include "JLog.h"

#include <QMenu>
#include <QFile>
#include <QHeaderView>

JProjectView::JProjectView(QWidget *parent, QAction *addSTL, QAction *addJTS, QAction *removeSTL)
	: QTreeView(parent)
{
	setAnimated(true);
	setSelectionBehavior(QAbstractItemView::SelectItems);
	//setRootIsDecorated(false);
	//setExpanded()

	QStringList headers;
	headers << tr("File");

	//QFile file("default.txt");
	//file.open(QIODevice::ReadOnly);
	m_projectModel = new JProjectModel(headers); //file.readAll());
	//file.close();
	setModel(m_projectModel);

	for (int column = 0; column < m_projectModel->columnCount(); ++column)
		header()->setResizeMode(column, QHeaderView::ResizeToContents);
		//resizeColumnToContents(column);

	setContextMenuPolicy(Qt::CustomContextMenu);
	connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenu(const QPoint &)) );

	connect(selectionModel(),
		SIGNAL(selectionChanged(const QItemSelection &,
		const QItemSelection &)),
		this, SLOT(updateActions()));

	m_addSTL = addSTL;//new QAction("3D Model",this);
	m_addJTS = addJTS;//new QAction("Kinematics ", this);
	m_removeSTL = new QAction("Remove ", this);

	//connect(actionsMenu, SIGNAL(aboutToShow()), this, SLOT(updateActions()));
	//connect(m_addSTL, SIGNAL(triggered()), this, SLOT(insertChild()));
	//connect(insertColumnAction, SIGNAL(triggered()), this, SLOT(insertColumn()));
	connect(m_removeSTL, SIGNAL(triggered()), this, SLOT(removeRow()));
	//connect(removeColumnAction, SIGNAL(triggered()), this, SLOT(removeColumn()));
	//connect(m_addJTS, SIGNAL(triggered()), this, SLOT(insertChild()));

	updateActions();
}

JProjectView::~JProjectView()
{

}

void JProjectView::updateActions()
{
	bool hasSelection = !selectionModel()->selection().isEmpty();
	m_removeSTL->setEnabled(hasSelection);
	//removeColumnAction->setEnabled(hasSelection);

	bool hasCurrent = selectionModel()->currentIndex().isValid();
	//insert3DModel->setEnabled(hasCurrent);
	//insertColumnAction->setEnabled(hasCurrent);

	if (hasCurrent) {
		closePersistentEditor(selectionModel()->currentIndex());

		int row = selectionModel()->currentIndex().row();
		int column = selectionModel()->currentIndex().column();
		
		if (selectionModel()->currentIndex().parent().isValid())
		{
			//statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
			//Log.Post(tr("Position: (%1,%2)").arg(row).arg(column));

			m_addSTL->setEnabled(false);
			m_addJTS->setEnabled(false);

			if(!selectionModel()->currentIndex().child(row,column).isValid()){
				m_addJTS->setEnabled(true);
				//Log.Post("no child");
			}
		}
		else
		{
			//statusBar()->showMessage(tr("Position: (%1,%2) in top level").arg(row).arg(column));
			//Log.Post(tr("Position: (%1,%2) in top level").arg(row).arg(column));
			m_addSTL->setEnabled(true);
			m_addJTS->setEnabled(false);
			m_removeSTL->setEnabled(false);
		}
	}
}

void JProjectView::insertChild(const QString& name)
{
	QModelIndex index = selectionModel()->currentIndex();
	QAbstractItemModel *model = this->model();

	// if no column exists, insert the initial column
	if (model->columnCount(index) == 0) {
		if (!model->insertColumn(0, index))
			return;
	}

	// insert a new child
	if (!model->insertRow(0, index))
		return;

	// setup null data for child, REMOVE THIS
	for (int column = 0; column < model->columnCount(index); ++column) {
		QModelIndex child = model->index(0, column, index);
		model->setData(child, QVariant(name), Qt::EditRole);
		// if header does not exist, add header
		if (!model->headerData(column, Qt::Horizontal).isValid())
			model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
	}

	// select the inserted child
	selectionModel()->setCurrentIndex(model->index(0, 0, index), QItemSelectionModel::ClearAndSelect);

	updateActions();
}

bool JProjectView::insertColumn( const QModelIndex &parent /*= QModelIndex()*/ )
{
	QAbstractItemModel *model = this->model();
	int column = selectionModel()->currentIndex().column();

	// Insert a column in the parent item.
	bool changed = model->insertColumn(column + 1, parent);
	if (changed)
		model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"),
		Qt::EditRole);

	updateActions();

	return changed;
}

void JProjectView::insertRow()
{
	QModelIndex index = selectionModel()->currentIndex();
	QAbstractItemModel *model = this->model();

	if (!model->insertRow(index.row()+1, index.parent()))
		return;

	updateActions();

	for (int column = 0; column < model->columnCount(index.parent()); ++column) {
		QModelIndex child = model->index(index.row()+1, column, index.parent());
		model->setData(child, QVariant("[No data]"), Qt::EditRole);
	}
}

bool JProjectView::removeColumn( const QModelIndex &parent /*= QModelIndex()*/ )
{
	QAbstractItemModel *model = this->model();
	int column = selectionModel()->currentIndex().column();

	// Insert columns in each child of the parent item.
	bool changed = model->removeColumn(column, parent);

	if (!parent.isValid() && changed)
		updateActions();

	return changed;
}

void JProjectView::removeRow()
{
	QModelIndex index = selectionModel()->currentIndex();
	QAbstractItemModel *model = this->model();
	if (model->removeRow(index.row(), index.parent()))
		updateActions();
}

void JProjectView::contextMenu( const QPoint &pt )
{
	QMenu *ctxMenu = new QMenu;
	//ctxMenu->addAction(tr( "Add Model" ), this, SLOT( insertRow() ) );
	//ctxMenu->addAction(tr( "Add Kinematics "), this, SLOT( insertChild()) );

	QMenu *menu = new QMenu("Add",ctxMenu);
	ctxMenu->addAction(menu->menuAction());
	menu->addAction(m_addSTL);
	//menu->addSeparator();
	menu->addAction(m_addJTS);

	ctxMenu->addSeparator();
	ctxMenu->addAction(m_removeSTL);

	ctxMenu->exec(this->mapToGlobal(pt));
}

void JProjectView::setCurrentProject(const QString &name)
{
}